#!/bin/bash

scriptdir=`pwd`
echo $scriptdir

#init
mkdir ~/linlab4 2> /dev/null
cd ~/linlab4

# 1 ----
cat /etc/passwd | awk -F":" '{ print $1 }' > work4.log

# 2 ----
cat /etc/group | awk -F":" '{ print $1 }' >> work4.log 

# 3 ----
echo "You're under surveillance. Keep your head down and stay obedient. The world is run by freemasons!" > /etc/skel/we_are_watching_you.txt

# 4 ----
useradd --no-user-group --create-home --password canihascheeeezzzzburrrger u1 #KITTEH MAJIK

# 5 ----
groupadd g1 && usermod -g g1 u1 #slightly different approach than Bulat's

# 6 ----
grep 'g1' /etc/group | awk -F":" '{ print $1 }' >> work4.log

# 7 ----
userdel u1 --force && groupdel g1

# 8 ----
python -uc "print '\n'.join(['t%d:123456p%d:::::' % (i,i) for i in xrange(1,5)])" | newusers

# 9 ----
python -u $scriptdir/creepy_table.py >> work4.log 

# 10 ----
mkdir test4 2> /dev/null
cp work4.log test4/work4-1.log
cp work4.log test4/work4-2.log
cp work4.log test4/work4-3.log

# 11 ----
groupadd tempgrp
#adduser t1 tempgrp; adduser t2 tempgrp; adduser t3 tempgrp; adduser t4 tempgrp
echo 't1 t2 t3 t4 ' | xargs -i -d ' ' adduser {} tempgrp 2> /dev/null

#let's set up owners first...
ls test4 | xargs -i chown t1:tempgrp test4/{} #ls test4 | xargs chgrp testgrp
chown t2:tempgrp test4 #; chgrp tempgrp test4

#and change perms as required
ls test4 | xargs -i chmod 0640 test4/{} #rw-r-----
chmod 0750 test4 #rwxr-x---

# 12 ----
mkdir test5
chmod 1666 test5 #quite sticky now

# 13 ----
cp /bin/rm megarm
chown t2:tempgrp megarm
chmod ug+s megarm
echo -e '#!/bin/bash \n ./megarm -f test4/work4-2.log' > rmlog.sh
chown t2:tempgrp rmlog.sh
chmod ug+s rmlog.sh #don't get confused by t and s
 
# 14 ----
cp /bin/ls megals
chown t1:tempgrp megals
chmod ug+s megals
echo -e '#!/bin/bash \n ./megals -l test4' > listlogs.sh
chown t1:tempgrp listlogs.sh
chmod ug+s listlogs.sh

# 15 ----
ls -ARl . >> work4.log